/**
 * 初始化订单管理详情对话框
 */
var TOrderInfoDlg = {
    tOrderInfoData: {}
};

var is_ture=true;
var orders = [];


/**
 * 关闭此对话框
 */
TOrderInfoDlg.close = function () {
    parent.layer.close(window.parent.TOrder.layerIndex);
}

/**
 * 收集数据
 */
TOrderInfoDlg.collectData = function () {
    var orders = []
    $("input[name='feeId']").each(function () {
        var mFee_id = $(this).val();
        var mFee_Income = $('#' + mFee_id + "_ff").val();
        var mFee_money = $.trim($('#' + mFee_id + "_m").val());
        var mFee_settlement = $('#' + mFee_id + "_s").val();
        var mFee_pay_mode = $('#pay_mode').val();
        var dotId = $('#dotId').val();
        var orderId = $('#id').val();
        if(!isNaN(mFee_money)&&parseInt(mFee_money)<0){
            Feng.error("只能输入数字并且不能小于0");
            is_ture=false
             return ;
        }
        var order_fee = {
            fee_id: mFee_id,
            fee_Income: mFee_Income,
            fee_money: mFee_money,
            fee_settlement: mFee_settlement,
            fee_pay_mode: mFee_pay_mode,
            dot_id: dotId,
            order_id: orderId
        }
        var order = "{fee_id:" + mFee_id + ",fee_Income:" + mFee_Income + "," +
            "fee_money:" + mFee_money + ",fee_settlement:" + mFee_settlement +
            ",fee_pay_mode:" + mFee_pay_mode + ",dot_id:" + dotId + ",order_id:" + orderId + "}";
        orders.push(order);
        is_ture=true;

    });
    this.set('str', "[" + orders.join(",") + "]");
    this.set('order_id', $('#id').val());
    // alert(JSON.stringify($(orders)));
    // alert(orders.toString);



}
TOrderInfoDlg.addLine = function (mVal, obj) {

    if ($(obj).is(':checked')) {
        var fee_name = $('#' + mVal + "_labe").val();
        var p_type = $('#' + mVal + "_type").val();
        var p_object = $('#' + mVal + "_object").val();
        var m_object = "";
        var m_type = "";
        if (p_object.indexOf("3") != -1) {
            m_object = $('#consignee').val();
        } else if( (p_object.indexOf("2") != -1)){
            m_object = $('#sendDeptName').val();
        } else if( (p_object.indexOf("4") != -1)){
            m_object = $('#transfer_name').val();
        } else if( (p_object.indexOf("5") != -1)){
            m_object = $('#reallyUser').val();
        }else{
            m_object="";
        }
        if (p_type == 1) {
            m_type = "收入";
        } else {
            m_type = "支出";
        }
        var str = "<tr id='" + mVal + "_tr'>" +
            "<td>" + fee_name + "</td>" +
            "<td>" + m_type + " <input value=\"" + p_type + "\" id=\"" + mVal + "_ff\" type=\"hidden\" > </td>" +
            "<td>" + m_object + "<input value=\"" + mVal + "\" name=\"feeId\" type=\"hidden\" id=\"" + mVal + "\">" +
            "<input value=\"" + p_object + "\" type=\"hidden\" id=\"" + mVal + "_use_object\"></td>";

        if (p_object.indexOf("3") != -1) {
            str = str + "<td><input value=\"" + 0 + "\" name=\"money\" id='" + mVal + "_m' type=\"text\" onkeyup='TOrderInfoDlg.changeMoney(" + mVal + ")'></td>";
            str = str + "<td>        <select id='" + mVal + "_s' onchange='TOrderInfoDlg.Calculation()'>\n" +
                "                        <option value=\"0\">当场结算</option>\n" +
                "                        <option value=\"1\">等待月结</option>\n" +
                "                    </select></td>";
        } else {
            str = str + "<td><input value=\"" + 0 + "\" name=\"money\" id='" + mVal + "_m'  type=\"text\" ></td>";
            str = str + "<td>        <select id='" + mVal + "_s'>\n" +
                "                        <option value=\"0\">当场结算</option>\n" +
                "                        <option value=\"1\">等待月结</option>\n" +
                "                    </select></td>";
        }

        str = str + "<td><button class=\"btn-danger\" onclick=\"TOrderInfoDlg.delLine(" + mVal + ")\">删除</button></td>" +
            "</tr>";
        $("#table").append(str)
    } else {
        $("#" + mVal + "_tr").remove()
    }

    this.Calculation();
}
TOrderInfoDlg.delLine = function (mVal) {
    $('#' + mVal + "_check").removeAttr("checked");
    $("#" + mVal + "_tr").remove()
    this.Calculation();

}

TOrderInfoDlg.changeMoney = function (mVal) {
    this.Calculation();
}
TOrderInfoDlg.Calculation = function () {
    var mSum = 0;
    var mPay = 0;
    $("input[name='feeId']").each(function () {
        if ($('#' + $(this).val() + "_use_object").val().indexOf("3") != -1) {

            if ($('#' + $(this).val() + "_m").val() != undefined && $('#' + $(this).val() + "_m").val() != 'undefined') {
                mSum = mSum + parseFloat($('#' + $(this).val() + "_m").val());//合计
                var mType = $('#' + $(this).val() + "_s").val();
                if (mType == 0) {
                    mPay = mPay + parseFloat(parseFloat($('#' + $(this).val() + "_m").val()));
                }
            }
        }
    });
    $("#sz_money").val(mPay);
    $("#all_money").val(mSum);
    $("#gz_money").val(parseFloat(mSum - mPay).toFixed(2));
}


TOrderInfoDlg.changeGZ = function (mVal) {
    var str = mVal.split("_");
    if (str[0] == 0) {
        var sum = parseInt($("#sz_money").val()) + parseInt(str[1])
        $("#sz_money").val(sum);
        $("#gz_money").val(parseInt($("#all_money").val()) - sum);
    } else {
        var sum = parseInt($("#sz_money").val()) - parseInt(str[1])
        $("#sz_money").val(sum);
        $("#gz_money").val(parseInt($("#all_money").val()) - sum);
    }
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TOrderInfoDlg.set = function (key, val) {
    this.tOrderInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 提交添加
 */
var sc=null
TOrderInfoDlg.addSubmit = function () {
    this.collectData();
    if(!is_ture){
        return
    }
    sc= layer.confirm('确认修改？', {
        btn: ['取消', '确定']
    }, function(index, layero){
        layer.close(sc);
    }, function(index, layero){
        layer.close(sc);
        openPrint()

    });

}
function openPrint() {

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tOrder/refSuperOrderFee", function (data) {
        Feng.success("操作成功!");
        TOrderInfoDlg.refTable();
    }, function (data) {
        Feng.error("操作失败!" + data.responseJSON.message + "!");

    });
    ajax.set(TOrderInfoDlg.tOrderInfoData);
    ajax.start();
}
TOrderInfoDlg.refTable=function () {
    window.parent.aTable.refresh();
    TOrderInfoDlg.close();
}

$(function () {


    if ($('#is_transfer').val() == 1) {
        $('#modeType').val(3);
    } else {
        if ($('#mode').val() == 0) {
            $('#modeType').val(1);
        } else {
            $('#modeType').val(2);
        }
    }
    $('#modeType').attr("disabled", "disabled");
    $("input[name='feeId']").each(function () {
        $('#' + $(this).val() + "_check").attr("checked", "true");
        var t = $('#' + $(this).val() + "_t").val();
        $('#' + $(this).val() + "_s").val(t);
    });
    TOrderInfoDlg.Calculation();

    layui.use('layer', function () {

    })


});
